package com.example.recycler.bsr;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/3/16.
 */

public class BsWaterView extends View{
     Paint mPaint;
    Context mContext;
    Path mPath;
    int mWaveLength=900;
    int mWaveHeight=700;
    int distanceX=0;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (distanceX>mWaveLength) {
                        distanceX=0;
                    }
                    distanceX+=15;
                    invalidate();
                    mHandler.sendEmptyMessageDelayed(1,5);
                    break;
                default:
                    break;
            }
        }
    };

    public BsWaterView(Context context) {
        super(context);
    }

    public BsWaterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        intit(context);
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void intit(Context context) {
        mPaint=new Paint();
        mPath=new Path();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mContext=context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int halfWaveLength=mWaveLength/2;
        mPath.reset();
        mPath.moveTo(-mWaveLength+distanceX,mWaveHeight);
        for (int i=-mWaveLength;i<=getWidth()+mWaveLength;i+=mWaveLength){
            mPath.rQuadTo(halfWaveLength / 2, -100, halfWaveLength, 0);
            mPath.rQuadTo(halfWaveLength / 2, 100, halfWaveLength, 0);
        }
        mPath.lineTo(getWidth(),getHeight());
        mPath.lineTo(0,getHeight());
        mPath.close();
        canvas.drawPath(mPath,mPaint);
    }
    public void reset(){
        mPath.reset();
        distanceX = 0;
        mWaveHeight = 700;
        invalidate();
    }
    public void startAnima(){
        mHandler.sendEmptyMessage(1);
    }
}
